<div class="layuimini-container layui-anim layui-anim-upbit">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="jobNumberSearch" autocomplete="off" class="layui-input"
                                    id="jobNumber">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userNameSearch" autocomplete="off" class="layui-input"
                                    id="userName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <!--<button type="submit" class="layui-btn layui-btn-primary" lay-submit-->
                            <!--lay-filter="data-search-btn"-->
                            <!--style="width:120px;height:38px;"><i-->
                                    <!--class="layui-icon"></i> 搜 索</button>-->
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"
                                    style="margin-left:10px;width:90px;height:38px;border:none;outline:none;border-radius:6px;background-color:#007bff;color:white"><i
                                        class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <br>
        <div id="table" lay-filter="currentTableFilter">
            <div class="layui-btn-container" style="position: relative;">
                <!-- <button class="layui-btn layui-btn-sm data-add-btn" id="tongbuUser" > 同步用户 </button> -->
                <button type="button" class="layui-btn layui-btn-warm layui-btn-radius add-user"
                        style="border-radius: 2px;"><i class="layui-icon layui-icon-form"></i> 添加用户</button>

                <!-- <button type="submit" lay-submit="" class="layui-btn layui-btn-warm template-download"
                                    style="border-radius:6px;background-color:#26B99A;">
                                    <i class="fa fa-cloud-download"></i> 模板下载</button>
                <button type="submit" lay-submit="" class="layui-btn layui-btn-warm batch-import" id="upload"
                                    style="border-radius:6px;background-color:#1e9fff;">
                                    <i class="fa fa-cloud-upload"></i> 批量导入</button> -->
            </div>
        </div>
        <div id="page"></div>
    </div>
</div>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="resetPwd">重置密码</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal data-count-edit" lay-event="editInfo">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-delete-btn" lay-event="deleteInfo">删除</a>
</script>

<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="muteFlag" value="{{d.id}}" title="禁言" lay-filter="lockDemo" {{ d.muteFlag =='1' ? 'checked' : 'disabled' }}>
  </script>



<script>
    var tableBar =[
        { title: '序 号', type: 'numbers', width: '5%' ,align: "center"},
        { field: 'account', title: '用 户 名', width: '15%',align: "center"},
        { field: 'user_name', title: '姓 名', width: '15%',align: "center"},
        { field: 'user_type', title: '管 理 员', width: '15%',align: "center",
            templet : function(d) {
                var t = d.user_type;
                if(t==0){
                    return '管理员'
                }else if(t==1) {
                    return '否'
                }
        }	},
        // { field: 'state', title: '员工状态', minwidth: '7%',align: "center" },
        // { field: 'showDepartment', title: '所 属 部 门',width: '30%',align: "center" },
        { field: 'phonenumber', title: '电 话', width: '15%',align: "center" },
        { field: 'email', title: '邮 箱', width: '15%' ,align: "center"},
        { title: '操 作', width: '20%', templet: '#currentTableBar',align: "center" }
        // ,{field:'muteFlag', title:'是 否 禁 言', width: '15%', templet: '#checkboxTpl', unresize: true}
    ]

    var initTableParams = {
        // "ID":"",
        // "name":"",
        // "orderType":"姓名",
        // "orderValue":"asc",
        // "page":1,
        // "pageSize":10
        "account":"",
        "user_name":"",
        "page":1,
        "pageSize":10
    }


    layuiEx.tableRender(
        "/admin/user/list", 
        'table', 
        'page', 
        tableBar,
        initTableParams
        );


    //检索事件
    layui.form.on('submit(data-search-btn)',function (params) {
        layuiEx.tableRender(
        "/admin/user/list", 
        'table', 
        'page', 
        tableBar,
        {
            // "ID":params.field.jobNumberSearch,
            // "name":params.field.userNameSearch,
            // "orderType":"姓名",
            // "orderValue":"asc",
            // "page":1,
            // "pageSize":10
            "account":params.field.jobNumberSearch,
            "user_name":params.field.userNameSearch,
            "page":1,
            "pageSize":10
        }
        );
        return false
    })


    layui.table.on('tool(currentTableFilter)', function(obj){
    var data = obj.data;
    if(obj.event === 'resetPwd'){
        // layer.confirm('重置密码为初始值?', function (index) {
        //         shared.ajaxPost('/adminUserInfo/resetPwd', { "id": data.id, "pwd": "value" }, function callback(res) {
        //         layer.msg(res)
        //             })
        //         });
        layer.prompt(
            {
                // formType: 2,
                // value: '默认值',
                title: '输入新密码',
                //   area: ['800px', '350px'] //自定义文本域宽高
            },
            function(value, index, elem){
                // alert(value); //得到value
                
                shared.ajaxPost('/admin/user/resetPwd', { "account": data.account, "newPwd": value }, function callback(res) {
                    layer.msg(res)
                    layer.close(index);
                })
        });
    }
    else if(obj.event === 'editInfo'){
        loadAdduserPage("edit",data)
    }
    else if(obj.event === 'deleteInfo'){
        layer.confirm('确定删除 ' +'<b>'+ obj.data.user_name +'</b>' + " ?", function (index) {
            deleteUser(data)
        })
        
        
    }
  });


    layui.form.on('checkbox(lockDemo)', function(obj){

        var id = obj.value
        // var newState = obj.elem.checked
        $.ajax({
            async : false,
            url: config.server + '/adminUserInfo/unmute',
            data:JSON.stringify({ "id": id}),
            type:"POST",
            headers: {
                    token: shared.getCookie('token'),
                 },
            crossDomain: true,
            cache:false,
            contentType: 'application/json',
            //contentType: 'text/html',
            dataType: "json",
            success: function (jResult) {
                
                if (jResult.code == 200) {
                    layer.msg(jResult.msg)
                    layuiEx.tableRender(
                    "/admin/user/list",
                    'table',
                    'page',
                    tableBar,
                    {
                        // "ID":$("#jobNumber").val(),
                        // "name":$("#userName").val(),
                        // "orderType":"姓名",
                        // "orderValue":"asc",
                        // "page":$(".layui-laypage-em").next().html(),
                        // "pageSize":10
                        "account":$("#jobNumber").val(),
                        "user_name":$("#userName").val(),
                        "page":$(".layui-laypage-em").next().html(),
                        "pageSize":10
                    }
                );
                }else{
                    layer.msg(jResult.msg,{icon: 2})
                    // console.log(obj.elem.checked)
                    // if (obj.elem.checked) {
                    //     obj.elem.checked = false
                    // }else{
                    //     obj.elem.checked = true
                    // }
                    // layui.form.render()
                    obj.elem.checked = !obj.elem.checked
                    layui.form.render()
                }
            },
            error: function (p1,p2,p3) {

            }
        })
        // shared.ajaxPost('/adminUserInfo/mute', { "id": id, "newState": newState }, function callback(res) {
        //         layer.msg(res)
        //     })
    });


    $(".add-user").on("click", function (obj) {
        loadAdduserPage("add")
        // layer.closeAll()
    });

    function loadAdduserPage(addORedit,data){
        var content = layui.miniPage.getHrefContent('mypages/usual/userMgt/addUser.html');
        var openWH = layui.miniPage.getOpenWidthHeight();
        var title = ""
        if (addORedit=="add") {
            title = "添加用户"
        }else{
            title = "编辑用户"
        }
        //不能用 var 前缀
        data_userMgr = data
        var index = layer.open({
            title: title,
            type: 1,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            offset: '100px',
            area : [ '500px', 'auto' ],
            // area: [openWH[0] / 2 + 'px', openWH[1] / 2 - 30 + 'px'],
            // offset: [openWH[2] + 'px', openWH[3] + 'px'],
            content: content,
            end: function () {
                var trunToPage = 1
                if (addORedit=="add") {
                    
                }else{
                    trunToPage = $(".layui-laypage-em").next().html()
                }
                layuiEx.tableRender(
                    "/admin/user/list",
                    'table',
                    'page',
                    tableBar,
                    {
                        // "ID":$("#jobNumber").val(),
                        // "name":$("#userName").val(),
                        // "orderType":"姓名",
                        // "orderValue":"asc",
                        // "page":trunToPage,
                        // "pageSize":10
                        "account":$("#jobNumber").val(),
                        "user_name":$("#userName").val(),
                        "page":trunToPage,
                        "pageSize":10
                    }
                );
            }
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
        return false;
    }

    function deleteUser(data){
        id = data.id
        shared.ajaxPost('/admin/user/deleteUser', { "account": data.account}, function callback(res) {
            if (res) {
                layer.msg(res, {
                    icon: 1,
                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    layuiEx.tableRender(
                    "/admin/user/list",
                    'table',
                    'page',
                    tableBar,
                    {
                        // "ID":$("#jobNumber").val(),
                        // "name":$("#userName").val(),
                        // "orderType":"姓名",
                        // "orderValue":"asc",
                        // "page":1,
                        // "pageSize":10
                        "account":$("#jobNumber").val(),
                        "user_name":$("#userName").val(),
                        "page":1,
                        "pageSize":10
                    }
                    );
                });
            }
        })
              

    }

    /**
    下载用户导入模板
    */
    $(".template-download").on("click", function (obj) {
        var url = config.server + "/admin/common/getTemplate?tp=user";
        window.location.href = url;
    });

    /*
    上传批量导入用户的excel
    */
    layui.upload.render({ //允许上传的文件后缀
        headers: { token: shared.getCookie('token') },
        elem: '#upload'
        , url: config.server + '/adminUserInfo/handleBatchImportUser'
        , before: function (obj) {
            // this.data = { 'dbCode': $("#dbCode").val().split("_")[0] };
            // showloading(true);
        }
        , accept: 'file' //普通文件
        , exts: 'xlsx' //只允许上传压缩文件
        , size: 1050000//设置文件最大可允许上传的大小1G，单位 KB。不支持ie8/9
        , done: function (res) {
            if (res.code != 200){
                layer.confirm(res.msg,{
                    btn: ['确定'],icon: 2, title:'信息'
                }, function () {
                    layer.closeAll();
                });
            }
            else{
                // 刷新页面
                layer.msg(res.msg, {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            layer.closeAll();
                            layuiEx.tableRender(
                                "/admin/user/list",
                                'table',
                                'page',
                                tableBar,
                                {
                                    // "ID":$("#jobNumber").val(),
                                    // "name":$("#userName").val(),
                                    // "orderType":"姓名",
                                    // "orderValue":"asc",
                                    // "page":1,
                                    // "pageSize":10
                                    "account":$("#jobNumber").val(),
                                    "user_name":$("#userName").val(),
                                    "page":1,
                                    "pageSize":10
                                }
                            );
                    });
                    
            }
        }, error: function () {
        }

    });
</script>